/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cestum.cave.ui.components {
import flash.events.MouseEvent;

import spark.components.Button;Button;
import spark.components.Panel;

public class InnerLogoPanel extends CavePanel {
	protected var uncollapsedPercentWidth:Number = NaN;
	protected var uncollapsedExplicitWidth:Number = NaN;

	
	
	[SkinState("collapsed")]
	public function InnerLogoPanel() 	{
		super();
	}
	[SkinPart(required="false")]
	/**
	 *  The skin part that defines the appearance of the 
	 *  button responsible for collapsing/uncollapsing the panel.
	 */
	public var collapseButton:ImageButton;
	
	/**
	 * Flag indicating whether this panel is collapsed (minimized) or not.
	 */ 
	public function get collapsed():Boolean 	{
		return _collapsed;
	}
	/**
	 * @private
	 */
	public function set collapsed(value:Boolean):void 	{
		if (value)
		{
			uncollapsedExplicitWidth = explicitWidth;
			uncollapsedPercentWidth = percentWidth;
			explicitWidth = NaN;
			percentWidth = NaN;
		}
		else
		{
			explicitWidth = uncollapsedExplicitWidth;
			percentWidth = uncollapsedPercentWidth;
		}
		_collapsed = value;
		invalidateSkinState();

	}
	
	/**
	 * @private
	 * Toggle collapsed state on collapseButton click event.
	 */
	protected function collapseButtonClickHandler(event:MouseEvent):void
	{
		collapsed = !collapsed;

	}
	
	/**
	 * @private
	 * storage variable for <code>collapsed</code>property.
	 * Add collapeButton click listener.
	 */
	protected var _collapsed:Boolean;
	
	override protected function partAdded(partName:String, instance:Object) : void	{
		super.partAdded(partName, instance);
		
		if (instance == collapseButton)
		{
			ImageButton(instance).addEventListener(MouseEvent.CLICK, collapseButtonClickHandler);
		}
	}
	
	/**
	 * @private
	 * Remove collapeButton click listener.
	 */
	override protected function partRemoved(partName:String, instance:Object) : void {
		if (instance == collapseButton)
		{
			ImageButton(instance).removeEventListener(MouseEvent.CLICK, collapseButtonClickHandler);
		}
		super.partRemoved(partName, instance);
	}
	
	/**
	 *  @private
	 */
	override protected function getCurrentSkinState():String {
		return collapsed ? "collapsed" : super.getCurrentSkinState();
	}
}
}